package com.sky1324.myspringboot.controller;

import com.sky1324.myspringboot.entity.sysUser;
import com.sky1324.myspringboot.service.sysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/sys")
public class sysController {

//      导入mapper
    @Autowired
    private com.sky1324.myspringboot.mapper.sysUserMapper sysUserMapper;
//    查询全部
    @GetMapping("/")
    public List<sysUser> init(){
        List<sysUser> all =  sysUserMapper.findAll();
        return all;
    }
    @Autowired
    private sysUserService sysUsersrvice;
// 插入数据
    @PostMapping("/adduser")
    public Integer save(@RequestBody sysUser sysuser){
        System.out.println("进入=======》");
            return sysUsersrvice.save(sysuser);
    }
//    删除
//    @DeleteMapping("/{id]")
        @DeleteMapping("/{id}")
    public Integer delete(@PathVariable Integer id){
            System.out.println("进入=======》" +id);
        return sysUsersrvice.del(id);
    }

//分页查询
//    查询全部
    /*
    * RequestParam 接受参数     pageNum = 1 & pagfeSize = 10
    *
    * */
@GetMapping("/page")
public Map<String,Object> finpage(@RequestParam Integer pageNum,
                                  @RequestParam Integer pageSize,
                                  @RequestParam String username) {
    pageNum = (pageNum-1) * pageSize;
    List<sysUser> data = sysUsersrvice.selecrPage(pageNum,pageSize,username);
    Integer total =  sysUsersrvice.selecatTotal(username);
    System.out.println("total===>" +total);
    Map<String,Object> res = new HashMap<>();
    res.put("data",data);
    res.put("total",total);
    return  res;

}
}
